package cn.lijiancn01.demo.business.menu.biz;

import cn.lijiancn01.demo.business.menu.model.MenuDO;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.EmptySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MenuBiz {

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    public List<MenuDO> list() {
        String sql = """
                select
                    mnu_code as id,
                    mnu_parent_menu_code as pid,
                    mnu_name as label,
                    mnu_type as type,
                    mnu_url as url                    
                from menu 
                where is_delete = 'N'
                """;
        return jdbcTemplate.query(sql, EmptySqlParameterSource.INSTANCE, new BeanPropertyRowMapper<>(MenuDO.class));
    }

    public List<MenuDO> list2() {
        String sql = new SQL() {{
            SELECT("""
                    mnu_code as id,
                    mnu_parent_menu_code as pid,
                    mnu_name as label,
                    mnu_type as type,
                    mnu_url as url
                    """);
            FROM("MENU");
            WHERE("is_delete = 'N'");
        }}.toString();
        return jdbcTemplate.query(sql, EmptySqlParameterSource.INSTANCE, new BeanPropertyRowMapper<>(MenuDO.class));
    }
}
